<?php
class AttendanceController extends Zend_Controller_Action 
{
    function init()
    {
        $this->initView();
        Zend_Loader::loadClass('AuxDbTable');
        Zend_Loader::loadClass('User');
        Zend_Loader::loadClass('Group');
        Zend_Loader::loadClass('Authgroup');
        Zend_Loader::loadClass('Mainmenu');
        Zend_Loader::loadClass('Rights');
        Zend_Loader::loadClass('Authority');
        Zend_Loader::loadClass('Userstudygroup');
        Zend_Loader::loadClass('Lesson');
        Zend_Loader::loadClass('Attendance');
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->user = Zend_Auth::getInstance()->getIdentity();
    }
    
    function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
        $menu = new Mainmenu();
        $this->view->main_menu = $menu->getMenu($this->view->user->id);
    }

    function indexAction()
    {
        $Rights = new Rights();
        //if(!$Rights->CanDo($this->view->user->id,'group','edit')) $this->_redirect(DEFAUL_URL);
        
        $lesson_id = (int)$this->_request->getParam('lesson', 0);
        $lesson = new Lesson();
        $this->view->lesson = $lesson->fetchRow('id='.$lesson_id);
        
        $group_id = (int)$this->view->lesson->studygroup_id;
        $group = new Group();
        $this->view->group = $group->fetchRow('id='.$group_id);
        
        $this->view->title = "Lesson ".$this->view->lesson->start_date.": Group ".$this->view->group->name." : Attendance";
        $this->view->content_title = "Lesson ".$this->view->lesson->start_date.": Group ".$this->view->group->name." : Attendance";
        
        $links = new Attendance();
        $this->view->linked = $links->getLinkedUser($lesson_id,$group_id);
        $user = new User();
        $this->view->users = $user->fetchAll('deleted!=1 
                                              and authgroup_id=1 
                                              and id not in (select user_id from attendance where lesson_id="'.$lesson_id.'")
                                              and id not in (select user_id from userstudygroup where studygroup_id="'.$group_id.'")','last_name');
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            
            $selected = $this->_request->getPost('users');
            foreach($selected as $user_id){
                $links->LinkUser($lesson_id,$user_id);
            }
            $this->_redirect('/attendance/index/lesson/'.$lesson_id);
            exit();
        }
        $this->view->can_add = true; //$Rights->CanDo($this->view->user->id,'group','add');
        $this->view->can_edit = true; //$Rights->CanDo($this->view->user->id,'group','edit');
        $this->view->can_delete = true; //$Rights->CanDo($this->view->user->id,'group','delete');
        
        $this->render();
    }
    
    function deleteAction()
    {
        $Rights = new Rights();
        //if(!$Rights->CanDo($this->view->user->id,'group','edit')) $this->_redirect(DEFAUL_URL);
        
        $lesson_id = (int)$this->_request->getParam('lesson', 0);
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            $links = new Attendance();
            $links->ClearLinks($lesson_id);
            $selected = $this->_request->getPost('linked');
            foreach($selected as $user_id){
                $links->linkUser($lesson_id,$user_id);
            }
            $this->_redirect('/attendance/index/lesson/'.$lesson_id);
            exit();
        }
        
    }
    
    function groupAction()
    {
        $Rights = new Rights();
        //if(!$Rights->CanDo($this->view->user->id,'group','edit')) $this->_redirect(DEFAUL_URL);
        
        $group_id = (int)$this->_request->getParam('id', 0);
        $lesson = new Lesson();
        $this->view->lesson = $lesson->fetchRow('studygroup_id='.$group_id);
        
        $group = new Group();
        $this->view->group = $group->fetchRow('id='.$group_id);
        
        $this->view->title = "Group ".$this->view->group->name." : Attendance";
        $this->view->content_title = "Group ".$this->view->group->name." : Attendance";
        
        $links = new Attendance();
        $this->view->linked = $links->getGroupAttendance($group_id);
        
        $this->view->linkeduserids = $this->view->links = array();
        foreach($this->view->linked as $l){
            $this->view->linkeduserids[] = $l['user_id'];
            $this->view->links[$l['start_date'].' '.$l['start_time']][$l['user_id']] = 1;
        }
        
        $Userstudygroup = new Userstudygroup();
        $Userstudygroups = $Userstudygroup->fetchAll('studygroup_id='.$group_id);
        
        foreach($Userstudygroups as $l){
            $this->view->linkeduserids[] = $l->user_id;
        }
        
        $ls = new Lesson();
        $this->view->lessons = $ls->fetchAll('studygroup_id='.$group_id.' AND deleted!=1', array('start_date','start_time'));
        
        $user = new User();
        if(count($this->view->linkeduserids)>0) {
            $this->view->users = $user->fetchAll('id IN ('.implode(',',$this->view->linkeduserids).') ','last_name');
        } else {
            $this->view->users = array();
        }
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            
            $selected = $this->_request->getPost('users');
            foreach($selected as $user_id){
                $links->LinkUser($lesson_id,$user_id);
            }
            $this->_redirect('/attendance/index/lesson/'.$lesson_id);
            exit();
        }
        $this->view->can_add = true; //$Rights->CanDo($this->view->user->id,'group','add');
        $this->view->can_edit = true; //$Rights->CanDo($this->view->user->id,'group','edit');
        $this->view->can_delete = true; //$Rights->CanDo($this->view->user->id,'group','delete');
        
        $this->render();
    }
    
    function userAction()
    {
        $Rights = new Rights();
        //if(!$Rights->CanDo($this->view->user->id,'group','edit')) $this->_redirect(DEFAUL_URL);
        
        $user_id = (int)$this->_request->getParam('id', 0);
        $user = new User();
        $this->view->c_user = $user->fetchRow('id='.$user_id);
        
        $ls = new Lesson();
        $group = new Userstudygroup();
        $groups = $group->fetchAll('user_id='.$user_id);
        
        $groupN = new Group();
        $this->view->groupNames = $groupN->getNames();
        
        $this->view->groups = array();
        $this->view->lessons = array();
        foreach($groups as $gr) {
            $lessons = $ls->fetchAll('studygroup_id='.$gr->studygroup_id.' AND deleted!=1', array('start_date','start_time')); 
            $this->view->groups[$gr->studygroup_id] = $lessons;
            foreach($lessons as $l) {
                $this->view->lessons[] = $l->id;
            }
        }
        
        $this->view->title = "User ".$this->view->first_name." ".$this->view->last_name." : Attendance";
        $this->view->content_title = "User ".$this->view->first_name." ".$this->view->last_name." : Attendance";
        
        $links = new Attendance();
        $this->view->links = $links->getLessonsByUser($user_id);
        
        $abs = array();
        foreach($this->view->links as $link) {
            if(!in_array($link,$this->view->lessons)) {
                $abs[] = $link;
            }
        }
        
        if(count($abs)>0) {
            $tmp = $links->getLessons($abs);
            foreach($tmp as $gr=>$lessons) {
                $this->view->groups[$gr] = $lessons;
                foreach($lessons as $l) {
                    $this->view->lessons[] = $l->id;
                }
            }            
        }
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            
            $selected = $this->_request->getPost('users');
            foreach($selected as $user_id){
                $links->LinkUser($lesson_id,$user_id);
            }
            $this->_redirect('/attendance/index/lesson/'.$lesson_id);
            exit();
        }
        $this->view->can_add = true; //$Rights->CanDo($this->view->user->id,'group','add');
        $this->view->can_edit = true; //$Rights->CanDo($this->view->user->id,'group','edit');
        $this->view->can_delete = true; //$Rights->CanDo($this->view->user->id,'group','delete');
        
        $this->render();
    }
}